package com.hushui.mall.commodity.service.impl;

import com.hushui.mall.commodity.entity.AttrAttrgroupRelationEntity;
import com.hushui.mall.commodity.entity.AttrEntity;
import com.hushui.mall.commodity.entity.vo.AttrGroupVo;
import com.hushui.mall.commodity.entity.vo.AttrGroupWithAttrVo;


import com.hushui.mall.commodity.service.AttrAttrgroupRelationService;
import com.hushui.mall.commodity.service.AttrService;
import com.hushui.mall.commodity.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.Query;

import com.hushui.mall.commodity.dao.AttrGroupDao;
import com.hushui.mall.commodity.entity.AttrGroupEntity;
import com.hushui.mall.commodity.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService service;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0){
            ArrayList<Long> longs = new ArrayList<>();
            longs.add(0L);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<>());
            IPage<AttrGroupVo> convert = page.convert(a -> {
                AttrGroupVo attrGroupVo = new AttrGroupVo();
                BeanUtils.copyProperties(a, attrGroupVo);
                attrGroupVo.setCatelogPath(longs);
                return attrGroupVo;
            });
            return new PageUtils(convert);
        }else {
            ArrayList<Long> path = attrService.getPath(catelogId);
            String key = (String)params.get("key");
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(key)){
                wrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page=this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            IPage<AttrGroupVo> convert = page.convert(a -> {
                AttrGroupVo attrGroupVo = new AttrGroupVo();
                BeanUtils.copyProperties(a, attrGroupVo);
                attrGroupVo.setCatelogPath(path);
                return attrGroupVo;
            });
            return new PageUtils(convert);
        }
    }

    @Override
    public PageUtils queryAttrGroupPage(Map<String, Object> params, Long attrgroupId) {
        if (attrgroupId == 0){
            IPage<AttrEntity> page = service.page(new Query<AttrEntity>().getPage(params), new QueryWrapper<AttrEntity>());
            return new PageUtils(page);
        }else {
            String key = (String)params.get("key");
            QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_group_id",attrgroupId);
            List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(queryWrapper);
            ArrayList<Long> lists = new ArrayList<>();
            for (AttrAttrgroupRelationEntity a:list
                 ) {lists.add(a.getAttrId());

            }
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(key)){
                wrapper.and((obj)->{
                    obj.in("attr_id",lists).eq("attr_id",key).or().like("attr_name",key).or().like("value_select",key);
                });
            }
            IPage<AttrEntity> page=service.page(new Query<AttrEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> list = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrVo> collect = list.stream().map(group -> {
            AttrGroupWithAttrVo attrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(group, attrVo);
            List<AttrAttrgroupRelationEntity> list1 = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrVo.getAttrGroupId()));
            ArrayList<Long> longs = new ArrayList<>();
            for (AttrAttrgroupRelationEntity a:list1
                 ) {longs.add(a.getAttrId());
            }
            attrVo.setAttrs(attrService.listByIds(longs));
            return attrVo;
        }).collect(Collectors.toList());
        return collect;
    }


    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

        return vos;
    }

}